﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.IO;
using System.Web.SessionState;

namespace HardwareAs
{
    public partial class Downloads : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

            if (!IsPostBack)
            {

                SqlRequest sr = new SqlRequest();
                CategoryTitle.DataTextField = "CategoryName";
                CategoryTitle.DataValueField = "Id";
                SqlDataReader reader = sr.AllCategory();
                CategoryTitle.DataSource = reader;
                CategoryTitle.DataBind();
                CategoryTitle.Items.Insert(0, "-Select the product-");
                reader.Close();
                CategoryTitle1.DataTextField = "CategoryName";
                CategoryTitle1.DataValueField = "Id";
                CategoryTitle1.DataSource = sr.AllCategory();
                CategoryTitle1.DataBind();
                CategoryTitle1.Items.Insert(0, "-Select the product-");
                DownloadTitle.Items.Insert(0, "-Select the Download-");

            }
            
        }

        protected void CategoryTitle_SelectedIndexChanged(object sender, EventArgs e)
        {
            string id=CategoryTitle.SelectedValue;
            if (!(id.Equals("-Select the product-")))
            {
                SqlRequest sr = new SqlRequest();
            DownloadTitle.DataTextField = "Title";
            DownloadTitle.DataValueField = "Id";
            DownloadTitle.DataSource = sr.AddDownload(CategoryTitle.SelectedValue);
            DownloadTitle.DataBind();
            DownloadTitle.Items.Insert(0, "-Select the Download-");
            }

        }

        protected void Search_click(object sender, EventArgs e)
        {
            SqlRequest sr = new SqlRequest();
            DownTitle.Text =DownloadTitle.SelectedItem.Text;
            Description.Text = sr.getDownloadDescription(DownloadTitle.SelectedValue);
            Button3.Enabled = true;
           
            
        }
       
        protected void Button2_Click(object sender, EventArgs e)
        {
            GetUrl.SaveAs(Request.PhysicalApplicationPath + "download//" + GetUrl.FileName);
            Label2.Visible = true;
            SqlRequest sr = new SqlRequest();
            String cid = CategoryTitle1.SelectedValue;
            String url = "download/"+GetUrl.FileName;
            String title = TextBox1.Text;
            String des = TextBox2.Text;
            String uid = (String)Session["username"];
            sr.InsertDownload(cid,title,des,url,uid);

        }

        public void DownLoad(string pathName)
        {
            string FilePath = Server.MapPath(pathName);
            if (System.IO.File.Exists(FilePath))
            {
                FileInfo file = new FileInfo(FilePath);
                Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");
                Response.AddHeader("Content-Disposition", "attachment; filename=" + Server.UrlEncode(file.Name));   
                Response.AddHeader("Content-length", file.Length.ToString());
                Response.ContentType = "appliction/octet-stream";
                Response.WriteFile(file.FullName);
                Response.End();

            }
        }


        protected void Button1_Click(object sender, EventArgs e)
        {
            SqlRequest sr=new SqlRequest();
            String url = Request.PhysicalApplicationPath + sr.getDownloadUrl(DownloadTitle.SelectedValue);
            System.IO.FileInfo file = new System.IO.FileInfo(url);
            Response.Clear();
            Response.AddHeader("Content-Disposition", "attachment;   filename=" + HttpUtility.UrlEncode(file.FullName));
            Response.AddHeader("Content-Length", file.Length.ToString());
            Response.ContentType = "application/octet-stream";
            Response.WriteFile(file.FullName);
            Response.Flush();
            Response.Clear();
            Response.End();



        }

        

      

        

        

       
}
   


}